/*
 Copyright 2011 and onwards Joao C. R. Pereira

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

      Unless required by applicable law or agreed to in writing, software
      distributed under the License is distributed on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      See the License for the specific language governing permissions and
      limitations under the License.
*/

#include <string.h>
#include "JumpTable.h"

void JumpTable_Clean(JumpTableInMemory * mem) {

  memset(mem, 0, sizeof(JumpTableInMemory));
}

void JumpTable_Optimize(JumpTableInMemory * mem) {

  int i,k;
  k=0;
  for(i=0;i<256;i++) {

    if (mem->jumps[i]) {
      mem->lookup[i] = k;
      k++;
    }
  }
  mem->numJumps = k;
}

void JumpTable_ToDisk(JumpTableInMemory * mem, JumpTableOnDisk * disk) {

  disk->bit000 = mem->jumps[0];
  disk->bit001 = mem->jumps[1];
  disk->bit002 = mem->jumps[2];
  disk->bit003 = mem->jumps[3];
  disk->bit004 = mem->jumps[4];
  disk->bit005 = mem->jumps[5];
  disk->bit006 = mem->jumps[6];
  disk->bit007 = mem->jumps[7];
  disk->bit008 = mem->jumps[8];
  disk->bit009 = mem->jumps[9];
  disk->bit010 = mem->jumps[10];
  disk->bit011 = mem->jumps[11];
  disk->bit012 = mem->jumps[12];
  disk->bit013 = mem->jumps[13];
  disk->bit014 = mem->jumps[14];
  disk->bit015 = mem->jumps[15];
  disk->bit016 = mem->jumps[16];
  disk->bit017 = mem->jumps[17];
  disk->bit018 = mem->jumps[18];
  disk->bit019 = mem->jumps[19];
  disk->bit020 = mem->jumps[20];
  disk->bit021 = mem->jumps[21];
  disk->bit022 = mem->jumps[22];
  disk->bit023 = mem->jumps[23];
  disk->bit024 = mem->jumps[24];
  disk->bit025 = mem->jumps[25];
  disk->bit026 = mem->jumps[26];
  disk->bit027 = mem->jumps[27];
  disk->bit028 = mem->jumps[28];
  disk->bit029 = mem->jumps[29];
  disk->bit030 = mem->jumps[30];
  disk->bit031 = mem->jumps[31];
  disk->bit032 = mem->jumps[32];
  disk->bit033 = mem->jumps[33];
  disk->bit034 = mem->jumps[34];
  disk->bit035 = mem->jumps[35];
  disk->bit036 = mem->jumps[36];
  disk->bit037 = mem->jumps[37];
  disk->bit038 = mem->jumps[38];
  disk->bit039 = mem->jumps[39];
  disk->bit040 = mem->jumps[40];
  disk->bit041 = mem->jumps[41];
  disk->bit042 = mem->jumps[42];
  disk->bit043 = mem->jumps[43];
  disk->bit044 = mem->jumps[44];
  disk->bit045 = mem->jumps[45];
  disk->bit046 = mem->jumps[46];
  disk->bit047 = mem->jumps[47];
  disk->bit048 = mem->jumps[48];
  disk->bit049 = mem->jumps[49];
  disk->bit050 = mem->jumps[50];
  disk->bit051 = mem->jumps[51];
  disk->bit052 = mem->jumps[52];
  disk->bit053 = mem->jumps[53];
  disk->bit054 = mem->jumps[54];
  disk->bit055 = mem->jumps[55];
  disk->bit056 = mem->jumps[56];
  disk->bit057 = mem->jumps[57];
  disk->bit058 = mem->jumps[58];
  disk->bit059 = mem->jumps[59];
  disk->bit060 = mem->jumps[60];
  disk->bit061 = mem->jumps[61];
  disk->bit062 = mem->jumps[62];
  disk->bit063 = mem->jumps[63];
  disk->bit064 = mem->jumps[64];
  disk->bit065 = mem->jumps[65];
  disk->bit066 = mem->jumps[66];
  disk->bit067 = mem->jumps[67];
  disk->bit068 = mem->jumps[68];
  disk->bit069 = mem->jumps[69];
  disk->bit070 = mem->jumps[70];
  disk->bit071 = mem->jumps[71];
  disk->bit072 = mem->jumps[72];
  disk->bit073 = mem->jumps[73];
  disk->bit074 = mem->jumps[74];
  disk->bit075 = mem->jumps[75];
  disk->bit076 = mem->jumps[76];
  disk->bit077 = mem->jumps[77];
  disk->bit078 = mem->jumps[78];
  disk->bit079 = mem->jumps[79];
  disk->bit080 = mem->jumps[80];
  disk->bit081 = mem->jumps[81];
  disk->bit082 = mem->jumps[82];
  disk->bit083 = mem->jumps[83];
  disk->bit084 = mem->jumps[84];
  disk->bit085 = mem->jumps[85];
  disk->bit086 = mem->jumps[86];
  disk->bit087 = mem->jumps[87];
  disk->bit088 = mem->jumps[88];
  disk->bit089 = mem->jumps[89];
  disk->bit090 = mem->jumps[90];
  disk->bit091 = mem->jumps[91];
  disk->bit092 = mem->jumps[92];
  disk->bit093 = mem->jumps[93];
  disk->bit094 = mem->jumps[94];
  disk->bit095 = mem->jumps[95];
  disk->bit096 = mem->jumps[96];
  disk->bit097 = mem->jumps[97];
  disk->bit098 = mem->jumps[98];
  disk->bit099 = mem->jumps[99];
  disk->bit100 = mem->jumps[100];
  disk->bit101 = mem->jumps[101];
  disk->bit102 = mem->jumps[102];
  disk->bit103 = mem->jumps[103];
  disk->bit104 = mem->jumps[104];
  disk->bit105 = mem->jumps[105];
  disk->bit106 = mem->jumps[106];
  disk->bit107 = mem->jumps[107];
  disk->bit108 = mem->jumps[108];
  disk->bit109 = mem->jumps[109];
  disk->bit110 = mem->jumps[110];
  disk->bit111 = mem->jumps[111];
  disk->bit112 = mem->jumps[112];
  disk->bit113 = mem->jumps[113];
  disk->bit114 = mem->jumps[114];
  disk->bit115 = mem->jumps[115];
  disk->bit116 = mem->jumps[116];
  disk->bit117 = mem->jumps[117];
  disk->bit118 = mem->jumps[118];
  disk->bit119 = mem->jumps[119];
  disk->bit120 = mem->jumps[120];
  disk->bit121 = mem->jumps[121];
  disk->bit122 = mem->jumps[122];
  disk->bit123 = mem->jumps[123];
  disk->bit124 = mem->jumps[124];
  disk->bit125 = mem->jumps[125];
  disk->bit126 = mem->jumps[126];
  disk->bit127 = mem->jumps[127];
  disk->bit128 = mem->jumps[128];
  disk->bit129 = mem->jumps[129];
  disk->bit130 = mem->jumps[130];
  disk->bit131 = mem->jumps[131];
  disk->bit132 = mem->jumps[132];
  disk->bit133 = mem->jumps[133];
  disk->bit134 = mem->jumps[134];
  disk->bit135 = mem->jumps[135];
  disk->bit136 = mem->jumps[136];
  disk->bit137 = mem->jumps[137];
  disk->bit138 = mem->jumps[138];
  disk->bit139 = mem->jumps[139];
  disk->bit140 = mem->jumps[140];
  disk->bit141 = mem->jumps[141];
  disk->bit142 = mem->jumps[142];
  disk->bit143 = mem->jumps[143];
  disk->bit144 = mem->jumps[144];
  disk->bit145 = mem->jumps[145];
  disk->bit146 = mem->jumps[146];
  disk->bit147 = mem->jumps[147];
  disk->bit148 = mem->jumps[148];
  disk->bit149 = mem->jumps[149];
  disk->bit150 = mem->jumps[150];
  disk->bit151 = mem->jumps[151];
  disk->bit152 = mem->jumps[152];
  disk->bit153 = mem->jumps[153];
  disk->bit154 = mem->jumps[154];
  disk->bit155 = mem->jumps[155];
  disk->bit156 = mem->jumps[156];
  disk->bit157 = mem->jumps[157];
  disk->bit158 = mem->jumps[158];
  disk->bit159 = mem->jumps[159];
  disk->bit160 = mem->jumps[160];
  disk->bit161 = mem->jumps[161];
  disk->bit162 = mem->jumps[162];
  disk->bit163 = mem->jumps[163];
  disk->bit164 = mem->jumps[164];
  disk->bit165 = mem->jumps[165];
  disk->bit166 = mem->jumps[166];
  disk->bit167 = mem->jumps[167];
  disk->bit168 = mem->jumps[168];
  disk->bit169 = mem->jumps[169];
  disk->bit170 = mem->jumps[170];
  disk->bit171 = mem->jumps[171];
  disk->bit172 = mem->jumps[172];
  disk->bit173 = mem->jumps[173];
  disk->bit174 = mem->jumps[174];
  disk->bit175 = mem->jumps[175];
  disk->bit176 = mem->jumps[176];
  disk->bit177 = mem->jumps[177];
  disk->bit178 = mem->jumps[178];
  disk->bit179 = mem->jumps[179];
  disk->bit180 = mem->jumps[180];
  disk->bit181 = mem->jumps[181];
  disk->bit182 = mem->jumps[182];
  disk->bit183 = mem->jumps[183];
  disk->bit184 = mem->jumps[184];
  disk->bit185 = mem->jumps[185];
  disk->bit186 = mem->jumps[186];
  disk->bit187 = mem->jumps[187];
  disk->bit188 = mem->jumps[188];
  disk->bit189 = mem->jumps[189];
  disk->bit190 = mem->jumps[190];
  disk->bit191 = mem->jumps[191];
  disk->bit192 = mem->jumps[192];
  disk->bit193 = mem->jumps[193];
  disk->bit194 = mem->jumps[194];
  disk->bit195 = mem->jumps[195];
  disk->bit196 = mem->jumps[196];
  disk->bit197 = mem->jumps[197];
  disk->bit198 = mem->jumps[198];
  disk->bit199 = mem->jumps[199];
  disk->bit200 = mem->jumps[200];
  disk->bit201 = mem->jumps[201];
  disk->bit202 = mem->jumps[202];
  disk->bit203 = mem->jumps[203];
  disk->bit204 = mem->jumps[204];
  disk->bit205 = mem->jumps[205];
  disk->bit206 = mem->jumps[206];
  disk->bit207 = mem->jumps[207];
  disk->bit208 = mem->jumps[208];
  disk->bit209 = mem->jumps[209];
  disk->bit210 = mem->jumps[210];
  disk->bit211 = mem->jumps[211];
  disk->bit212 = mem->jumps[212];
  disk->bit213 = mem->jumps[213];
  disk->bit214 = mem->jumps[214];
  disk->bit215 = mem->jumps[215];
  disk->bit216 = mem->jumps[216];
  disk->bit217 = mem->jumps[217];
  disk->bit218 = mem->jumps[218];
  disk->bit219 = mem->jumps[219];
  disk->bit220 = mem->jumps[220];
  disk->bit221 = mem->jumps[221];
  disk->bit222 = mem->jumps[222];
  disk->bit223 = mem->jumps[223];
  disk->bit224 = mem->jumps[224];
  disk->bit225 = mem->jumps[225];
  disk->bit226 = mem->jumps[226];
  disk->bit227 = mem->jumps[227];
  disk->bit228 = mem->jumps[228];
  disk->bit229 = mem->jumps[229];
  disk->bit230 = mem->jumps[230];
  disk->bit231 = mem->jumps[231];
  disk->bit232 = mem->jumps[232];
  disk->bit233 = mem->jumps[233];
  disk->bit234 = mem->jumps[234];
  disk->bit235 = mem->jumps[235];
  disk->bit236 = mem->jumps[236];
  disk->bit237 = mem->jumps[237];
  disk->bit238 = mem->jumps[238];
  disk->bit239 = mem->jumps[239];
  disk->bit240 = mem->jumps[240];
  disk->bit241 = mem->jumps[241];
  disk->bit242 = mem->jumps[242];
  disk->bit243 = mem->jumps[243];
  disk->bit244 = mem->jumps[244];
  disk->bit245 = mem->jumps[245];
  disk->bit246 = mem->jumps[246];
  disk->bit247 = mem->jumps[247];
  disk->bit248 = mem->jumps[248];
  disk->bit249 = mem->jumps[249];
  disk->bit250 = mem->jumps[250];
  disk->bit251 = mem->jumps[251];
  disk->bit252 = mem->jumps[252];
  disk->bit253 = mem->jumps[253];
  disk->bit254 = mem->jumps[254];
  disk->bit255 = mem->jumps[255];
}

void JumpTable_ToMem(JumpTableOnDisk * disk, JumpTableInMemory * mem) {

  mem->jumps[0] = disk->bit000;
  mem->jumps[1] = disk->bit001;
  mem->jumps[2] = disk->bit002;
  mem->jumps[3] = disk->bit003;
  mem->jumps[4] = disk->bit004;
  mem->jumps[5] = disk->bit005;
  mem->jumps[6] = disk->bit006;
  mem->jumps[7] = disk->bit007;
  mem->jumps[8] = disk->bit008;
  mem->jumps[9] = disk->bit009;
  mem->jumps[10] = disk->bit010;
  mem->jumps[11] = disk->bit011;
  mem->jumps[12] = disk->bit012;
  mem->jumps[13] = disk->bit013;
  mem->jumps[14] = disk->bit014;
  mem->jumps[15] = disk->bit015;
  mem->jumps[16] = disk->bit016;
  mem->jumps[17] = disk->bit017;
  mem->jumps[18] = disk->bit018;
  mem->jumps[19] = disk->bit019;
  mem->jumps[20] = disk->bit020;
  mem->jumps[21] = disk->bit021;
  mem->jumps[22] = disk->bit022;
  mem->jumps[23] = disk->bit023;
  mem->jumps[24] = disk->bit024;
  mem->jumps[25] = disk->bit025;
  mem->jumps[26] = disk->bit026;
  mem->jumps[27] = disk->bit027;
  mem->jumps[28] = disk->bit028;
  mem->jumps[29] = disk->bit029;
  mem->jumps[30] = disk->bit030;
  mem->jumps[31] = disk->bit031;
  mem->jumps[32] = disk->bit032;
  mem->jumps[33] = disk->bit033;
  mem->jumps[34] = disk->bit034;
  mem->jumps[35] = disk->bit035;
  mem->jumps[36] = disk->bit036;
  mem->jumps[37] = disk->bit037;
  mem->jumps[38] = disk->bit038;
  mem->jumps[39] = disk->bit039;
  mem->jumps[40] = disk->bit040;
  mem->jumps[41] = disk->bit041;
  mem->jumps[42] = disk->bit042;
  mem->jumps[43] = disk->bit043;
  mem->jumps[44] = disk->bit044;
  mem->jumps[45] = disk->bit045;
  mem->jumps[46] = disk->bit046;
  mem->jumps[47] = disk->bit047;
  mem->jumps[48] = disk->bit048;
  mem->jumps[49] = disk->bit049;
  mem->jumps[50] = disk->bit050;
  mem->jumps[51] = disk->bit051;
  mem->jumps[52] = disk->bit052;
  mem->jumps[53] = disk->bit053;
  mem->jumps[54] = disk->bit054;
  mem->jumps[55] = disk->bit055;
  mem->jumps[56] = disk->bit056;
  mem->jumps[57] = disk->bit057;
  mem->jumps[58] = disk->bit058;
  mem->jumps[59] = disk->bit059;
  mem->jumps[60] = disk->bit060;
  mem->jumps[61] = disk->bit061;
  mem->jumps[62] = disk->bit062;
  mem->jumps[63] = disk->bit063;
  mem->jumps[64] = disk->bit064;
  mem->jumps[65] = disk->bit065;
  mem->jumps[66] = disk->bit066;
  mem->jumps[67] = disk->bit067;
  mem->jumps[68] = disk->bit068;
  mem->jumps[69] = disk->bit069;
  mem->jumps[70] = disk->bit070;
  mem->jumps[71] = disk->bit071;
  mem->jumps[72] = disk->bit072;
  mem->jumps[73] = disk->bit073;
  mem->jumps[74] = disk->bit074;
  mem->jumps[75] = disk->bit075;
  mem->jumps[76] = disk->bit076;
  mem->jumps[77] = disk->bit077;
  mem->jumps[78] = disk->bit078;
  mem->jumps[79] = disk->bit079;
  mem->jumps[80] = disk->bit080;
  mem->jumps[81] = disk->bit081;
  mem->jumps[82] = disk->bit082;
  mem->jumps[83] = disk->bit083;
  mem->jumps[84] = disk->bit084;
  mem->jumps[85] = disk->bit085;
  mem->jumps[86] = disk->bit086;
  mem->jumps[87] = disk->bit087;
  mem->jumps[88] = disk->bit088;
  mem->jumps[89] = disk->bit089;
  mem->jumps[90] = disk->bit090;
  mem->jumps[91] = disk->bit091;
  mem->jumps[92] = disk->bit092;
  mem->jumps[93] = disk->bit093;
  mem->jumps[94] = disk->bit094;
  mem->jumps[95] = disk->bit095;
  mem->jumps[96] = disk->bit096;
  mem->jumps[97] = disk->bit097;
  mem->jumps[98] = disk->bit098;
  mem->jumps[99] = disk->bit099;
  mem->jumps[100] = disk->bit100;
  mem->jumps[101] = disk->bit101;
  mem->jumps[102] = disk->bit102;
  mem->jumps[103] = disk->bit103;
  mem->jumps[104] = disk->bit104;
  mem->jumps[105] = disk->bit105;
  mem->jumps[106] = disk->bit106;
  mem->jumps[107] = disk->bit107;
  mem->jumps[108] = disk->bit108;
  mem->jumps[109] = disk->bit109;
  mem->jumps[110] = disk->bit110;
  mem->jumps[111] = disk->bit111;
  mem->jumps[112] = disk->bit112;
  mem->jumps[113] = disk->bit113;
  mem->jumps[114] = disk->bit114;
  mem->jumps[115] = disk->bit115;
  mem->jumps[116] = disk->bit116;
  mem->jumps[117] = disk->bit117;
  mem->jumps[118] = disk->bit118;
  mem->jumps[119] = disk->bit119;
  mem->jumps[120] = disk->bit120;
  mem->jumps[121] = disk->bit121;
  mem->jumps[122] = disk->bit122;
  mem->jumps[123] = disk->bit123;
  mem->jumps[124] = disk->bit124;
  mem->jumps[125] = disk->bit125;
  mem->jumps[126] = disk->bit126;
  mem->jumps[127] = disk->bit127;
  mem->jumps[128] = disk->bit128;
  mem->jumps[129] = disk->bit129;
  mem->jumps[130] = disk->bit130;
  mem->jumps[131] = disk->bit131;
  mem->jumps[132] = disk->bit132;
  mem->jumps[133] = disk->bit133;
  mem->jumps[134] = disk->bit134;
  mem->jumps[135] = disk->bit135;
  mem->jumps[136] = disk->bit136;
  mem->jumps[137] = disk->bit137;
  mem->jumps[138] = disk->bit138;
  mem->jumps[139] = disk->bit139;
  mem->jumps[140] = disk->bit140;
  mem->jumps[141] = disk->bit141;
  mem->jumps[142] = disk->bit142;
  mem->jumps[143] = disk->bit143;
  mem->jumps[144] = disk->bit144;
  mem->jumps[145] = disk->bit145;
  mem->jumps[146] = disk->bit146;
  mem->jumps[147] = disk->bit147;
  mem->jumps[148] = disk->bit148;
  mem->jumps[149] = disk->bit149;
  mem->jumps[150] = disk->bit150;
  mem->jumps[151] = disk->bit151;
  mem->jumps[152] = disk->bit152;
  mem->jumps[153] = disk->bit153;
  mem->jumps[154] = disk->bit154;
  mem->jumps[155] = disk->bit155;
  mem->jumps[156] = disk->bit156;
  mem->jumps[157] = disk->bit157;
  mem->jumps[158] = disk->bit158;
  mem->jumps[159] = disk->bit159;
  mem->jumps[160] = disk->bit160;
  mem->jumps[161] = disk->bit161;
  mem->jumps[162] = disk->bit162;
  mem->jumps[163] = disk->bit163;
  mem->jumps[164] = disk->bit164;
  mem->jumps[165] = disk->bit165;
  mem->jumps[166] = disk->bit166;
  mem->jumps[167] = disk->bit167;
  mem->jumps[168] = disk->bit168;
  mem->jumps[169] = disk->bit169;
  mem->jumps[170] = disk->bit170;
  mem->jumps[171] = disk->bit171;
  mem->jumps[172] = disk->bit172;
  mem->jumps[173] = disk->bit173;
  mem->jumps[174] = disk->bit174;
  mem->jumps[175] = disk->bit175;
  mem->jumps[176] = disk->bit176;
  mem->jumps[177] = disk->bit177;
  mem->jumps[178] = disk->bit178;
  mem->jumps[179] = disk->bit179;
  mem->jumps[180] = disk->bit180;
  mem->jumps[181] = disk->bit181;
  mem->jumps[182] = disk->bit182;
  mem->jumps[183] = disk->bit183;
  mem->jumps[184] = disk->bit184;
  mem->jumps[185] = disk->bit185;
  mem->jumps[186] = disk->bit186;
  mem->jumps[187] = disk->bit187;
  mem->jumps[188] = disk->bit188;
  mem->jumps[189] = disk->bit189;
  mem->jumps[190] = disk->bit190;
  mem->jumps[191] = disk->bit191;
  mem->jumps[192] = disk->bit192;
  mem->jumps[193] = disk->bit193;
  mem->jumps[194] = disk->bit194;
  mem->jumps[195] = disk->bit195;
  mem->jumps[196] = disk->bit196;
  mem->jumps[197] = disk->bit197;
  mem->jumps[198] = disk->bit198;
  mem->jumps[199] = disk->bit199;
  mem->jumps[200] = disk->bit200;
  mem->jumps[201] = disk->bit201;
  mem->jumps[202] = disk->bit202;
  mem->jumps[203] = disk->bit203;
  mem->jumps[204] = disk->bit204;
  mem->jumps[205] = disk->bit205;
  mem->jumps[206] = disk->bit206;
  mem->jumps[207] = disk->bit207;
  mem->jumps[208] = disk->bit208;
  mem->jumps[209] = disk->bit209;
  mem->jumps[210] = disk->bit210;
  mem->jumps[211] = disk->bit211;
  mem->jumps[212] = disk->bit212;
  mem->jumps[213] = disk->bit213;
  mem->jumps[214] = disk->bit214;
  mem->jumps[215] = disk->bit215;
  mem->jumps[216] = disk->bit216;
  mem->jumps[217] = disk->bit217;
  mem->jumps[218] = disk->bit218;
  mem->jumps[219] = disk->bit219;
  mem->jumps[220] = disk->bit220;
  mem->jumps[221] = disk->bit221;
  mem->jumps[222] = disk->bit222;
  mem->jumps[223] = disk->bit223;
  mem->jumps[224] = disk->bit224;
  mem->jumps[225] = disk->bit225;
  mem->jumps[226] = disk->bit226;
  mem->jumps[227] = disk->bit227;
  mem->jumps[228] = disk->bit228;
  mem->jumps[229] = disk->bit229;
  mem->jumps[230] = disk->bit229;
  mem->jumps[230] = disk->bit230;
  mem->jumps[231] = disk->bit231;
  mem->jumps[232] = disk->bit232;
  mem->jumps[233] = disk->bit233;
  mem->jumps[234] = disk->bit234;
  mem->jumps[235] = disk->bit235;
  mem->jumps[236] = disk->bit236;
  mem->jumps[237] = disk->bit237;
  mem->jumps[238] = disk->bit238;
  mem->jumps[239] = disk->bit239;
  mem->jumps[240] = disk->bit240;
  mem->jumps[241] = disk->bit241;
  mem->jumps[242] = disk->bit242;
  mem->jumps[243] = disk->bit243;
  mem->jumps[244] = disk->bit244;
  mem->jumps[245] = disk->bit245;
  mem->jumps[246] = disk->bit246;
  mem->jumps[247] = disk->bit247;
  mem->jumps[248] = disk->bit248;
  mem->jumps[249] = disk->bit249;
  mem->jumps[250] = disk->bit250;
  mem->jumps[251] = disk->bit251;
  mem->jumps[252] = disk->bit252;
  mem->jumps[253] = disk->bit253;
  mem->jumps[254] = disk->bit254;
  mem->jumps[255] = disk->bit255;
}



